<!-- 卡通图 -->
<template>
	<view class="">
		<view class="topBox">
			<view class="status_bar"></view>
			<uni-nav-bar
				style="width: 750rpx; height: 44px;"
				@clickRight="showSetUp"
				@clickLeft="goHome"
				color="#FDFDFD"
				:border="false"
				:shadow="false"
				background-color="#F1F1F1"
			>
				<view class="titleImg"><image style="width: 100%; height: 100%;" mode="scaleToFill" src="/static/icon/logo_ctw@3x.png"></image></view>
				<view slot="left">
					<view class="leftIconBox"><uni-icons type="arrowleft" color="#000" size="18"></uni-icons></view>
				</view>
				<view slot="right"><uni-icons type="bars" color="#7F7F7F" size="28"></uni-icons></view>
			</uni-nav-bar>
		</view>
		<view class="zhuti" :style="{ marginTop: topBar }"><view class="htmlBigbox" v-html="htmlBox"></view></view>
		<!-- 设置 -->
		<uni-popup ref="SetUpPopup" type="center" :animation="false"><set-up-pages ref="showPopup" @closePopUp="closePopUp" /></uni-popup>
		<!-- 退出 -->
		<sign-out ref="signOutBox" />
	</view>
</template>

<script>
import setUpPages from '@/pages/setUpPages/index.vue';
import signOut from '@/pages/setUpPages/signOut.vue'; // 退出登录
import { baseUrl } from '@/utils/http.js';
export default {
	data: function() {
		return {
			topBarHeight: 0,
			windowHeight: 0,
			back_k: 0, // 物理返回
			popupIsShow: false, // 打开设置的标识
			signOutShow: false, // 退出登录
			pages: 1,
			id: '',
			htmlBox: '',
			isHome: 'no'
		};
	},
	onLoad(option) {
		const { id, pages, isHome } = option;
		this.id = id;
		this.pages = pages;
		this.isHome = isHome;
		// 拦截物理返回键
		this.goBackBox();
		this.getDetails();
	},
	onUnload() {
		uni.$off('goBack_k', this.goBackBox2);
	},
	onHide() {
		if (this.$refs.SetUpPopup) {
			this.$refs.SetUpPopup.close();
		}
		if (this.$refs.signOutBox) {
			this.$refs.signOutBox.close();
		}
		this.popupIsShow = false;
	},
	created() {
		const that = this;
		uni.getSystemInfo({
			success(res) {
				that.topBarHeight = res.statusBarHeight;
				that.windowHeight = res.windowHeight;
			}
		});
	},
	components: { setUpPages, signOut },
	computed: {
		topBar() {
			let num = 0;
			num = this.topBarHeight + 44; // 44是顶部高度
			return num + 'px';
		},
		i18n() {
			return this.$t('cartoon');
		}
	},
	methods: {
		// 获取详情
		async getDetails() {
			const params = {
				infoId: this.id
			};
			const { data: res } = await this.$http(true, 'api/info/infoDetail', params, 'GET');
			const text = res.data.textWithoutPageBreak;
			// const city2 = text.replace(/src=\"/g, `src="${baseUrl}`);
			this.htmlBox = text;
		},
		goBackBox() {
			// const that = this;
			uni.$on('goBack_k', this.goBackBox2);
		},
		goBackBox2(data) {
			this.back_k < 0 ? (this.back_k = 0) : (this.back_k = this.back_k);
			if (data) {
				if (this.popupIsShow) {
					this.$refs.SetUpPopup.close();
					this.popupIsShow = false;
				} else if (this.signOutShow) {
					this.$refs.signOutBox.close();
					this.signOutShow = false;
				} else {
					let main = plus.android.runtimeMainActivity();
					if (this.back_k > 0) {
						this.back_k = 0;
						return main.moveTaskToBack(false);
						// return plus.runtime.quit();
					} else {
						const pages = getCurrentPages();
						if (pages.length === 1) {
							this.back_k++;
							setTimeout(() => {
								this.back_k--;
							}, 1000);
							if (this.isHome === 'no') {
								// #ifdef APP-PLUS
								uni.redirectTo({
									url: `/pages/Journalism/index?pages=${this.pages}`
								});
								// #endif
								// #ifdef H5
								this.$Router.push(`/pages/Journalism/index?pages=${this.pages}`);
								// #endif
							} else {
								// #ifdef APP-PLUS
								uni.redirectTo({
									url: '/pages/home/index'
								});
								// #endif
								// #ifdef H5
								this.$Router.push('/pages/home/index');
								// #endif
							}
							// uni.showToast({
							// 	title: '再按一次退出应用',
							// 	icon: 'none',
							// 	duration: 1000
							// });
						}
					}
				}
			}
		},
		// 返回home
		goHome() {
			if (this.isHome === 'no') {
				// #ifdef APP-PLUS
				uni.redirectTo({
					url: `/pages/Journalism/index?pages=${this.pages}`
				});
				// #endif
				// #ifdef H5
				this.$Router.push(`/pages/Journalism/index?pages=${this.pages}`);
				// #endif
			} else {
				// #ifdef APP-PLUS
				uni.redirectTo({
					url: '/pages/home/index'
				});
				// #endif
				// #ifdef H5
				this.$Router.push('/pages/home/index');
				// #endif
			}
			// // #ifdef APP-PLUS
			// uni.redirectTo({
			// 	url: `/pages/Journalism/index?pages=${this.pages}`
			// });
			// // #endif
			// // #ifdef H5
			// this.$Router.push(`/pages/Journalism/index?pages=${this.pages}`);
			// // #endif
		},
		// 打开设置
		showSetUp() {
			this.$refs.SetUpPopup.open('right');
			this.popupIsShow = true;
		},
		// 关闭设置
		closePopUp(val) {
			if (val) {
				this.$refs.SetUpPopup.close();
				this.popupIsShow = false;
			} else {
				this.$refs.SetUpPopup.close();
				this.popupIsShow = false;
				this.$refs.signOutBox.open();
				this.signOutShow = true;
			}
		}
	}
};
</script>

<style lang="scss" scoped>
::v-deep img {
	width: 100% !important;
}
::v-deep p {
	width: 100% !important;
	overflow: hidden;
}
::v-deep span {
	width: 100% !important;
	font-size: $uni-font-size-custom-d !important;
	overflow: hidden;
}
.titleImg {
	width: 60px;
	height: 30px;
	margin: 0 auto;
	border-radius: 50%;
	overflow: hidden;
}
.leftIconBox {
	width: 25px;
	height: 25px;
	background-color: #fff;
	border-radius: 50%;
	display: flex;
	justify-content: center;
	align-items: center;
	margin-left: 15px;
}
.topBox {
	position: fixed;
	top: 0;
	left: 0;
	z-index: 10;
	// 顶部部分
	.status_bar {
		height: var(--status-bar-height);
		background-color: #f1f1f1;
		width: 100%;
	}
}
.zhuti {
	overflow: scroll;
	.htmlBigbox {
		padding: 20px;
		box-sizing: border-box;
	}
}
</style>
